[NativeAOT] Print OOM message before Abort() on Linux#125311
Conversation
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
|
Please add a unit test to src/tests |
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Added a new |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
| // Minimal OOM fail-fast path: avoid heap allocations as much as possible, but still | ||
| // report that OOM is the reason for the crash. | ||
| try | ||
| { | ||
| // Try to print the same short message CoreCLR prints. | ||
| Internal.Console.Error.Write("Out of memory."); | ||
| Internal.Console.Error.WriteLine(); | ||
| } | ||
| catch { } |
There was a problem hiding this comment.
We may want to address this by changing Internal.Console.Error.Write to avoid managed allocations for small strings on Unix. Notice that Windows implementation is like that already.
I suspect that the test may be flaky otherwise given that it tries to allocate every last bit of managed memory.
On Linux, NativeAOT processes terminating due to
OutOfMemoryException(e.g. withDOTNET_GCHeapHardLimitset) printed onlyAbortedwith no diagnostic context.Root cause
RuntimeExceptionHelpers.FailFastdetects the preallocated OOM exception viaminimalFailFast = (exception == PreallocatedOutOfMemoryException.Instance)and skips all stderr output to avoid heap allocations — leaving the user with no indication of why the process died.Fix
In the
minimalFailFastpath, print a hardcoded OOM message to stderr before callingAbort(). The write is wrapped intry/catch {}so a secondary allocation failure silently falls through to the existing abort path.Before:
AbortedAfter:
Out of memory.Test
Added a new
OomHandlingsmoke test insrc/tests/baseservices/exceptions/OutOfMemoryException/. The test spawns itself as a subprocess withDOTNET_GCHeapHardLimit=20000000(32 MB) set, waits for the subprocess to run out of memory, and verifies thatOutOfMemoryExceptionappears in stderr. This covers both the preallocated OOM path (the fix) and the existing unhandled-exception path. The test is skipped on mobile and browser platforms that do not support process spawning.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.